package com.example.springvue.common.utils;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;

/**
 * @Author zhouaokai
 * @Description 公用类
 * @Date 13:52 2020/5/26
 */
public class CommonUtils {





    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress="";
        }
        // ipAddress = this.getRequest().getRemoteAddr();

        return ipAddress;
    }




    /**
     * @Description 拼接项目域名
     */
    public static String image(String url){
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();

        StringBuffer requestUrl = request.getRequestURL();

        //域名
        String domainName = requestUrl.delete(requestUrl.length() - request.getRequestURI().length(), requestUrl.length()).toString();

        //返回的拼接地址
        String imageUrl  = domainName+url;

        return imageUrl;
    }



    /**
     * @Description 判断字符串是否为空，为空返回false，不为空返回true。
     */
    public static boolean isNotEmptyString(String param){
        return param != null && !"".equals(param) && !"null".equals(param) && param.length() > 0;
    }
    /**
     * @Description 判断数据是否为空，为空返回false，不为空返回true。
     */
    public static boolean isNotEmptyInteger(int param){
        return param > 0;
    }
    /**
     * @Description 判断object是否为空，为空返回false，不为空返回true。
     */
    public static boolean isNotEmptyObject(Object param){
        return null != param && !"".equals(param) && !"null".equals(param);
    }
    /**
     * @Description 判断列表是否为空，为空返回false，不为空返回true。
     */
    public static <E> boolean isNotEmptyList(List<E> params){
        return params != null && params.size() > 0;
    }
    /**
     * @Description 判断数组是否为空，为空返回false，不为空返回true。
     */
    public static boolean isNotEmptyArray(Object[] params){
        return params != null && params.length > 0;
    }
    /**
     * @Description 判断集合是否为空，为空返回false，不为空返回true。
     */
    public static <E> boolean isNotEmptySet(Set<E> params){
        return params != null && !params.isEmpty();
    }
    /**
     * @Description 判断集合是否为空，为空返回false，不为空返回true。
     */
    public static <k, V> boolean isNotEmptyMap(Map<k,V> params){
        return params != null && !params.isEmpty() && params.size() > 0;
    }
    /**
     * @Description 判断实体中必传字段是否为空，为空返回false，不为空返回true。 注意：当变量为0是默认为空
     */
    public static <T> boolean isNotEmptyClass(T enety, String[] params) throws IllegalAccessException {
        Set<String> set = new HashSet<>(Arrays.asList(params));
        Class<?> enetyClass = enety.getClass();
        Field[] fields = enetyClass.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (set.contains(field.getName())){
                String value = field.get(enety).toString();
                if (value == null || "".equals(value) || "null".equals(value) || "0".equals(value)) {
                    return false;
                }
            }
        }
        return true;
    }
    /**
     * @Description 判断hashMap中一些key是否为空，为空返回false，不为空返回true。
     */
    public static <K,V> boolean isNotEmptyHashMapForKey(Map<K,V> map,String[] params){
        if (!isNotEmptyMap(map)){
            return false;
        }
        List<String> list = Arrays.asList(params);
        for (String keys : list) {
            if(!map.containsKey(keys) || !isNotEmptyObject(map.get(keys))){
                return false;
            }
        }
        return true;
    }

}
